package cn.lijiajia3515.cairo.system.modules.file;

import cn.lijiajia3515.cairo.auth.CairoAuthMeta;
import cn.lijiajia3515.cairo.auth.modules.account.Account;
import org.bson.types.ObjectId;

import java.util.Optional;

public class FileConverter {
	public static File convert(FileMongo m, Account createdUser, Account lastModifiedUser) {
		return File.builder()
			.id(Optional.ofNullable(m.get_id()).map(ObjectId::toHexString).orElse(null))
			.path(m.getPath())
			.filename(m.getFilename())
			.contentType(m.getMeta().get_contentType())
			.length(m.getLength())
			.md5(m.getMd5())
			.chuckSize(m.getChunkSize())
			.meta(CairoAuthMeta.builder()
				.created(CairoAuthMeta.Action.builder()
					.account(createdUser)
					.at(m.getMeta().getCreated().getAt())
					.build())
				.lastModified(CairoAuthMeta.Action.builder()
					.account(lastModifiedUser)
					.at(m.getMeta().getLastModified().getAt())
					.build())
				.build())
			.build();
	}
}
